global class AvgAccountContactCompleteBatch implements Database.Batchable<AggregateResult> {

    global AvgAccountContactCompleteBatch() {
       
    }
    global Iterable<AggregateResult> start(Database.batchableContext info){
       return [SELECT count(Id) ,AccountId FROM Contact 
              where  IncorporateCompleteness__c = true 
              and Account.Owner.Profile.Name != '系统管理员' group by AccountId  ];
    }


    global void execute(Database.BatchableContext BC, list<AggregateResult> scope ) {

        Set<String > accs= new Set<String>();
        for (AggregateResult  arg: scope) {
            accs.add((String )arg.get('AccountId'));
        }

        update [select Id from contact where AccountId in:accs and IncorporateCompleteness__c = true  ];
        // Set<Id>accset = new Set<Id>();
        // for(Account acc :scope){
        //     accset.add(acc.Id);
        // }
        // List<Account>  accountList = new List<Account>();
        // for(Account account :  [SELECT Id,RecordType.DeveloperName,
        //     (SELECT Id,CustomerCompleteness__c,DealerCompleteness__c FROM Contacts where  IncorporateCompleteness__c = true)
        //     FROM Account WHERE Id IN : accset   ] ){
        //     List<Contact> contacts = account.Contacts;
        //     Decimal completeness = 0;
        //         if(!contacts.isEmpty()){    
        //             for(Contact contact : contacts){
        //                 if(contact.CustomerCompleteness__c!=null){
        //                     completeness += contact.CustomerCompleteness__c  ;
        //                 }
        //             }
        //             completeness /= contacts.size();
        //         }
        //         account.CustomerContactInfoIntegrity__c = completeness;

        //         accountList.add(account);
        //     }


            
        // if(accountList.size() > 0)Database.update(accountList, false);

    }

    global void finish(Database.BatchableContext BC) {

    }
}